﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Admin_CauHoi_CapNhat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loaddulieu();
            setData();
        }
    }

    private void loaddulieu()
    {
        List<LoaiCauHoiINFO> dsloaicauhoi = LoaiCauHoiDAL.listAll();
        ddlloaicauhoi.DataSource = dsloaicauhoi;
        ddlloaicauhoi.DataTextField = "TenLoai";
        ddlloaicauhoi.DataValueField = "MaLoai";
        ddlloaicauhoi.DataBind();
        List<MonThiINFO> dsmonthi = MonThiDAL.listAll();
        ddlmonthi.DataSource = dsmonthi;
        ddlmonthi.DataTextField = "TenMon";
        ddlmonthi.DataValueField = "MaMon";
        DataBind();
        List<DoKhoINFO> dsdokho = DoKhoDAL.listAll();
        ddldokho.DataSource = dsdokho;
        ddldokho.DataTextField = "DoKho";
        ddldokho.DataValueField = "MaDoKho";
        DataBind();
    }

    private void setData() {
        int macauhoi = int.Parse(Request["macauhoi"]);
        CauHoiINFO mCauHoi = CauHoiDAL.GetInfo(macauhoi);
        List<DapAnINFO> dsDapAn = DapAnDAL.listByCauHoi(macauhoi);
        txtnoidungch.Text = mCauHoi.NoiDungCH;
        txtsoluachon.Text = mCauHoi.SoLuaChon.ToString();
        ddlloaicauhoi.SelectedValue = mCauHoi.MaLoai.ToString();
        ddlmonthi.SelectedValue = mCauHoi.MaMon.ToString();
        ddldokho.SelectedValue = mCauHoi.MaDoKho.ToString();
        StringBuilder sdapan = new StringBuilder();
        foreach (DapAnINFO item in dsDapAn)
        {
            sdapan.Append("<tr><td style='vertical-align:middle; width:30px'>");
            if(item.GiaTri == 1)
                sdapan.Append("<input type='checkbox' class='GiaTri' checked='checked'/>");
            else
                sdapan.Append("<input type='checkbox' class='GiaTri'>");
            sdapan.Append("</td>");
            sdapan.Append("<td style='vertical-align:middle'><textarea class='form-control NoiDung' rows='3' placeholder='Nhap dap An...'>").Append(item.NoiDungDA).Append("</textarea></td>");
            sdapan.Append("<td style='vertical-align:middle; width:50px'><button onclick='return XoaDapAn(this);' class='btn btn-default m-l-3' title='Xóa'><i class='fa fa-times'></i> </button></td></tr>");
        }
        ltrDapAn.Text = sdapan.ToString();
        txtMaCauHoi.Value = Request["macauhoi"];
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //int maloaicauhoi = int.Parse(ddlloaicauhoi.SelectedValue.ToString());
        //int mamonthi = int.Parse(ddlmonthi.SelectedValue.ToString());
        //string noidungcauhoi = txtnoidungch.Text;
        //int soluachon = int.Parse(txtsoluachon.Text);


        //CauHoiINFO obj = new CauHoiINFO();
        //obj.MaLoai = maloaicauhoi;
        //obj.MaMon = mamonthi;
        //obj.NoiDungCH = noidungcauhoi;
        //obj.SoLuaChon = soluachon;

        //if (CauHoiDAL.Insert(obj) > 0)
        //{
        //    Literal1.Text = "<p class='text-green'>Đã cập nhật dữ liệu!</p>";
        //}
        //else
        //{
        //    Literal1.Text = "<p class='text-red'>Không thể cập nhật dữ liệu!</p>";
        //}
    }
}